****************************************************************************************************************
**																											  **
** This do file is part of the replication material for the following article: 								  **
**  "Is There an Ideological Asymmetry in the Incumbency Effect? Evidence from U.S. Congressional Elections." **
** 		Authors: Morisi, Davide, Jost, John, Panagopoulos, Costas, and Jussi Valtonen		    			  **
** 		Journal: Social Psychological and Personality Science																				  **
**																											  **
** This file replicates the following step of the analysis: 												  **
**																											  **
**		FIGURES INCLUDED IN THE PAPER				   													  	  **
**																											  **
****************************************************************************************************************
*Set working directory
cd "C:\Users\Davide Morisi\Dropbox\datasets"

*Open merged dataset
use "CCES\CCES_merged_dataset9.dta", clear

set matsize 800

*Demographics
global demo3 "i.gender birthyr ib2.educ i.racer i.religpewr ib2.economy3"

*Additional controls for House elections
global control_house "i.held_offices2 i.freshman i.redist dexp10 rexp10"
fre held_offices2 freshman redist
fre held_offices2 freshman redist if filter_hou_new==1

*Filter out open seats/seats without 2 major parties in House & Senate
fre filter_hou_new
fre filter_sen1

*Do not use old var for house incumbent & state
rename inc_rep_hou OLD_inc_rep_hou
rename inc_all_hou OLD_inc_all_hou
rename inc_all_hou_filter OLD_inc_all_hou_f
rename own_incLCMpre_hou OLD_own_incLCMpre_hou
rename own_incDRIprof_hou OLD_own_incDRIprof_hou
rename state OLD_state
*New var
rename inc_rep_hou2 inc_rep_hou
rename inc_all_hou2 inc_all_hou
rename own_incLCMpre_hou2 own_incLCMpre_hou
rename own_incDRIprof_hou2 own_incDRIprof_hou
fre inc_all_hou // already filtered
fre inc_rep_hou if filter_hou_new==1

*Districts with alternation
egen mean_rep_inc = mean(inc_rep_hou) if filter_hou_new==1, by(stcd)
fre mean_rep_inc
*ta stcd inc_rep_hou if filter_hou_new==1 & mean_rep_inc==0 // districts with only dem inc
*ta stcd inc_rep_hou if filter_hou_new==1 & mean_rep_inc==1 // districts with only rep inc
ta stcd inc_rep_hou if filter_hou_new==1 & mean_rep_inc>0 & mean_rep_inc<1 // districts with both inc
gen filter_dist = 0 if mean_rep_inc!=.
replace filter_dist = 1 if mean_rep_inc>0 & mean_rep_inc<1
fre filter_dist // 37% districts with alternations


******************************
*FIGURES IN THE PAPER
*****************************


*************
*Figure 1. Incumbency effects as a function OF respondent ideology in Congressional elections (2006-2018)

*house
*model 2 in table A2
eststo clear
logit own_candLCMpre_hou i.ideo2pre##i.own_incLCMpre_hou $demo3 i.stcd i.year if filter_hou_new==1 [pweight=weight1]
eststo Liberals: margins 1.ideo2pre, dydx(own_incLCMpre_hou) post // .097 (.089 - .0106
logit own_candLCMpre_hou i.ideo2pre##i.own_incLCMpre_hou $demo3 i.stcd i.year if filter_hou_new==1 [pweight=weight1]
eststo Conservatives: margins 2.ideo2pre, dydx(own_incLCMpre_hou) post // .124 (.11 - .14)
coefplot (Liberals, ciopts(lcolor(navy)) mcolor(navy) msymbol(O) msize(medlarge)) ///
(Conservatives, ciopts(lcolor(maroon)) mcolor(maroon) msymbol(D) msize(medium)), ///
vert yline(0) yscale(range(0(.02).14)) ylabel(0(.02).14) ///
ytitle("Incumbency effect") title("House") legend(off) ///
graphregion(color(white)) name(figure1a, replace)

*senate
*model 2 in table A4
eststo clear
logit own_candLCMpre_sen i.ideo2pre##i.own_incLCMpre_sen $demo3 i.staten i.year if filter_sen1==1 [pweight=weight1]
eststo Liberals_sen: margins 1.ideo2pre, dydx(own_incLCMpre_sen) post // .04
logit own_candLCMpre_sen i.ideo2pre##i.own_incLCMpre_sen $demo3 i.staten i.year if filter_sen1==1 [pweight=weight1]
eststo Conservatives_sen: margins 2.ideo2pre, dydx(own_incLCMpre_sen) post // .08
coefplot (Liberals_sen, ciopts(lcolor(navy)) mcolor(navy) msymbol(O) msize(medlarge)) ///
 (Conservatives_sen, ciopts(lcolor(maroon)) mcolor(maroon) msymbol(D) msize(medium)), ///
vert yline(0) yscale(range(0(.02).14)) ylabel(0(.02).14) ///
ytitle("") title("Senate") legend(off) ///
graphregion(color(white)) name(figure1b, replace)

*combined
graph combine figure1a figure1b, graphregion(color(white)) name(figure1)



********
*Figure 2. Incumbency effects by strength of ideology in Congressional elections (2006-2018)

fre ideo6pre
recode ideo6pre 5=4 6=5 7=6, gen(ideo6prer)
label de ideo6prer 1"Very liberals" 2"Liberals" 3"Somewhat liberals" 4"Somewhat conservatives" 5"Conservatives" 6"Very conservatives"
label val ideo6prer ideo6prer
fre ideo6prer

*house
logit own_candLCMpre_hou c.ideo6prer##c.ideo6prer##i.own_incLCMpre_hou $demo3 i.stcd i.year if filter_hou_new==1 [pweight=weight1]
margins, dydx(own_incLCMpre_hou) at(ideo6prer==(1(1)6)) post
marginsplot, name(house)
*very lib 		0.030 (0.004)
*lib			0.089 (0.005)
*somewhat lib	0.140 (0.005)
*somewhat cons	0.151 (0.006)
*cons			0.130 (0.006)
*very cons		0.082 (0.007)
*extreme cons vs extreme lib > significant
*cons vs  lib > significant
*somewhat cons vs somewhat lib > z=-1.410141, The P-Value is .15854 (two tailed)

*senate
logit own_candLCMpre_sen c.ideo6prer##c.ideo6prer##i.own_incLCMpre_sen $demo3 i.staten i.year if filter_sen1==1 [pweight=weight1]
margins, dydx(own_incLCMpre_sen) at(ideo6prer==(1(1)6)) post
marginsplot, name(senate)
*very lib 		0.007 (0.003)
*lib			0.045 (0.006)
*somewhat lib	0.105 (0.006)
*somewhat cons	0.124 (0.008)
*cons			0.082 (0.008)
*very cons		0.016 (0.008)
*test of difference for somewhat lib vs somewhat cons
*z=-2.010526 // The P-Value is .044431 (two tailed)

*combined
graph combine house senate, ycommon graphregion(color(white)) name(fig2, replace)



**********
*Figure 3. Incumbency effects by knowledge of House Representative (House elections, 2006-2018)

*knowledge of congressman
fre approval_house
recode approval_house (1/4=0 "knows candidate") (5=1 "dk"), gen(no_knowledge_house)
fre no_knowledge_house

*no knowledge
eststo clear
logit own_candLCMpre_hou i.ideo2pre##i.own_incLCMpre_hou##i.no_knowledge_house $demo3  i.stcd i.year if filter_hou_new==1 [pweight=weight1]
eststo lib_no: margins 1.ideo2pre, dydx(own_incLCMpre_hou) at(no_knowledge_house==1) post 
logit own_candLCMpre_hou i.ideo2pre##i.own_incLCMpre_hou##i.no_knowledge_house $demo3  i.stcd i.year if filter_hou_new==1 [pweight=weight1]
eststo con_no: margins 2.ideo2pre, dydx(own_incLCMpre_hou) at(no_knowledge_house==1) post 

coefplot (lib_no, ciopts(lcolor(navy)) mcolor(navy) msymbol(O) msize(medlarge)) ///
(con_no, ciopts(lcolor(maroon)) mcolor(maroon) msymbol(D) msize(medium)), ///
vert yline(0, lcolor(gs10) lpatter(dash)) ///
ytitle("Incumbency effect") title("Doesn't know House Representative") legend(off) ///
graphregion(color(white)) name(figure2a, replace)

*knowledge
logit own_candLCMpre_hou i.ideo2pre##i.own_incLCMpre_hou##i.no_knowledge_house $demo3  i.stcd i.year if filter_hou_new==1 [pweight=weight1]
eststo lib_know: margins 1.ideo2pre, dydx(own_incLCMpre_hou) at(no_knowledge_house==0) post 
logit own_candLCMpre_hou i.ideo2pre##i.own_incLCMpre_hou##i.no_knowledge_house $demo3  i.stcd i.year if filter_hou_new==1 [pweight=weight1]
eststo con_know: margins 2.ideo2pre, dydx(own_incLCMpre_hou) at(no_knowledge_house==0) post 

coefplot (lib_know, ciopts(lcolor(navy)) mcolor(navy) msymbol(O) msize(medlarge)) ///
(con_know, ciopts(lcolor(maroon)) mcolor(maroon) msymbol(D) msize(medium)), ///
vert yline(0, lcolor(gs10) lpatter(dash)) ///
ytitle("") title("Knows House Representative") legend(off) ///
graphregion(color(white)) name(figure2b, replace)

*combined
graph combine figure2a figure2b, ycommon graphregion(color(white)) name(fig3, replace)



**********
*Figure 4. Incumbency effects by ideology and competitive districts in House elections (2006-2018)

sum partygap01
sum partygap, d

logit own_candLCMpre_hou i.ideo2pre##i.own_incLCMpre_hou##c.partygap01 $demo3 i.stcd i.year if filter_hou_new==1 [pweight=weight1]
margin ideo2pre, dydx(own_incLCMpre_hou) at(partygap01==(0(0.1)1))

marginsplot, yline(0) name(figure4, replace) recastci(rline) ciopts(lpattern(dash)) recast(line) ///
graphregion(color(white)) ///
ytitle("Incumbency effect") ///
xtitle("Vote gap between two major parties""in previous election (0=min, 1=max)") 









